//
//  SunProductDetailVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/19.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunProductDetailVC.h"
#import "SunLoginVC.h"
#import "SunUseCardsVC.h"
#import "SunWebVC.h"
#import "SunImgShows.h"
#import "SunMyOrderVC.h"
#import "JxWebVC.h"
#import "SunDelegateWebVC.h"
#import "XSPInvestmentVC.h"
#import "ColorButton.h"
#undef Y
#define Y(y) NY(y)
#define SafeAreaBottomHeight (SCREENHEIGHT == 812.0 ? 34 : 0)

@interface SunProductDetailVC ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UITextFieldDelegate>
{
    UILabel *_basePercent;
    UIView *_barLine;
    UILabel *_days;
    UIView *_progressView;
    UILabel *_progressLable;
    UILabel *_total;
    UILabel *_leave;
    UITableView *_tb;
    UIButton *_pay;
    NSDictionary *_borrowInfo;
    NSDictionary *_borrowDetail;
    BOOL _canUseProfit;
    NSMutableArray *_sessionArr;
    BOOL _showDetail;
    int _selectNum;
    UIView *_profitView;
    UIView *_detailView;
    NSMutableArray *_recordArr;
    UITextField *_userInput;
    UILabel *_userInputProfit;
    NSMutableArray *_imgArr;
    NSMutableArray *_cards;
    NSDictionary *_chooseCard;
    UIView *_checkView;
    //    UIView *_payView;
    Sun *_sunAlert;
    NSTimer *_timer;
    int _time;
    NSString *_tradeNo;
    float _rulerValue;
    int _imgNum1;
    int _imgNum2;
    UILabel *_titleLabel;
}
@property (nonatomic, assign) NSInteger canUseCard;
@property (nonatomic, strong) UIImageView *progressImage;
@property (nonatomic, strong) NSString *balanceMoney;

@property (nonatomic, assign) NSInteger isRisk;
@property (nonatomic, strong) Sun *sunAlert;
@property (nonatomic, strong) UIView *riskView;

@property (nonatomic, strong) UIWebView *safeView;

@property (nonatomic, assign) int keyHeight;
@property (nonatomic, strong) UIView *grayView;
@property (nonatomic, strong) UIButton *calculateBtn;

@property (nonatomic, strong) UILabel *timeWord;
@property (nonatomic, strong) UILabel *wayWord;
@property (nonatomic, strong) UILabel *beginTime;
@property (nonatomic, strong) UILabel *fullTime;
@property (nonatomic, strong) UILabel *closeTime;
@property (nonatomic, strong) UILabel *repayTime;

@property (nonatomic, strong) NSMutableArray *specialRecord;


@end

@implementation SunProductDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    self.specialRecord = [NSMutableArray array];
    [self initData];
    [self creatUI];
    [[Sun shareSun]showActivity:self];
    //    [self firstToHelp];
    self.sunAlert = [[Sun alloc]init];
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    [self requestToBorrowRecord];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [self requestToBorrowInfo];
    if ([[Sun getUser:KLOGIN] intValue] == 1) {
        [self requestToUser];
    }
    _showDetail = YES;
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

//- (void)firstToHelp{
//    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"detailhlep"]) {
//        UIImageView *img = [[UIImageView alloc]init];
//        img.image = [UIImage imageNamed:@"second_detailhelp"];
//        [[XSP shareSun]show:img to:[UIApplication sharedApplication].keyWindow shouldReturn:YES];
//        [img lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
//        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"detailhlep"];
//    }
//}

- (void)initData{
    _imgArr = [NSMutableArray array];
    _sunAlert = [[Sun alloc]init];
}

#pragma mark- 键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (![_userInput isExclusiveTouch]) {
        [_userInput resignFirstResponder];
    }
    return YES;
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    self.keyHeight = keyboardRect.size.height;
    [_profitView lt_setBottom:self.keyHeight+X(42) to:nil save:YES];
    [self.grayView setFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-self.keyHeight-X(42)-X(120))];
    self.grayView.hidden = NO;
    _profitView.hidden = NO;
    [_pay setFrame:CGRectMake(X(42), self.view.frame.size.height - X(42)-self.keyHeight, SCREENWIDTH - X(42), X(42))];
    [self.calculateBtn setFrame:CGRectMake(0, self.view.frame.size.height - X(47)-self.keyHeight, X(48), X(47))];
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    [self hideProfitView];
}
- (void)hideProfitView{
    _profitView.hidden = YES;
    self.grayView.hidden = YES;
    [_pay setFrame:CGRectMake(X(42), self.view.frame.size.height - X(42)-SafeAreaBottomHeight, SCREENWIDTH - X(42), X(42))];
    [self.calculateBtn setFrame:CGRectMake(0, self.view.frame.size.height - X(47)-SafeAreaBottomHeight, X(48), X(47))];
}
#pragma mark- creatProfitView
- (void)creatProfitView{
    self.grayView = [[UIView alloc]init];
    [self.view addSubview:self.grayView];
    self.grayView.backgroundColor = BLACK;
    self.grayView.alpha = 0.2;
    self.grayView.hidden = YES;
    _profitView = [[UIView alloc]init];
    _profitView.hidden = YES;
    [self.view addSubview:_profitView];
    _profitView.backgroundColor = [UIColor whiteColor];
    [_profitView lt_setSize:CGSizeMake(SCREENWIDTH, X(120)) save:YES];
    [_profitView lt_XcenterTo:nil save:YES];
    UILabel *pLabel = [[UILabel alloc]init];
    [_profitView addSubview:pLabel];
    [pLabel lt_setLeft:X(12) to:nil save:YES];
    [pLabel lt_setTop:X(15) to:nil save:YES];
    pLabel.textColor = [UIColor colorWithHexString:@"050505"];
    pLabel.font = [UIFont systemFontOfSize:12];
    double allapr = [[_borrowInfo objectForKey:@"aprBase"]doubleValue]+[[_borrowInfo objectForKey:@"aprAdd"] doubleValue];
    pLabel.text = [NSString stringWithFormat:@"预计年化:%.2f%%",allapr];
    UILabel *dLabel = [[UILabel alloc]init];
    [_profitView addSubview:dLabel];
    [dLabel lt_setRight:X(12) to:nil save:YES];
    [dLabel lt_YcenterTo:pLabel save:YES];
    dLabel.textAlignment = NSTextAlignmentRight;
    dLabel.textColor = [UIColor colorWithHexString:@"050505"];
    dLabel.font = [UIFont systemFontOfSize:12];
    dLabel.text = [NSString stringWithFormat:@"投资期限:%@%@",[_borrowInfo objectForKey:@"days"],[_borrowInfo objectForKey:@"time"]];
    
    UIView *line1 = [[UIView alloc]init];
    [_profitView addSubview:line1];
    [line1 lt_setTop:X(41) to:nil save:YES];
    [line1 lt_setLeft:X(12) to:nil Right:X(12) to:nil save:YES];
    [line1 lt_setHeight:1 save:YES];
    line1.backgroundColor = [UIColor colorWithHexString:@"D8D8D8"];
    
    UILabel *label1 = [[UILabel alloc]init];
    [_profitView addSubview:label1];
    [label1 lt_setLeft:X(12) to:nil save:YES];
    //    [label1 lt_setTop:X(52) to:nil save:YES];
    [label1 lt_YcenterTo:nil save:YES];
    label1.textColor = [UIColor colorWithHexString:@"050505"];
    label1.font = [UIFont systemFontOfSize:12];
    label1.text = @"投资金额";
    //    label1.tag = 789;
    _userInput = [[UITextField alloc]init];
    [_profitView addSubview:_userInput];
    [_userInput lt_YcenterTo:label1 save:YES];
    [_userInput lt_setRight:X(12) to:nil save:YES];
    [_userInput lt_setSize:CGSizeMake(SCREENWIDTH/2, 25) save:YES];
    _userInput.textAlignment = NSTextAlignmentRight;
    _userInput.textColor = [UIColor colorWithHexString:@"050505"];
    _userInput.font = [UIFont systemFontOfSize:12];
    _userInput.keyboardType = UIKeyboardTypeNumberPad;
    _userInput.placeholder = [NSString stringWithFormat:@"起投金额%@元",[_borrowInfo objectForKey:@"limitMoney"]];
    _userInput.delegate = self;
    UIView *line = [[UIView alloc]init];
    [_profitView addSubview:line];
    [line lt_setTop:X(79) to:nil save:YES];
    [line lt_setLeft:X(12) to:nil Right:X(12) to:nil save:YES];
    [line lt_setHeight:1 save:YES];
    line.backgroundColor = [UIColor colorWithHexString:@"D8D8D8"];
    UILabel *label2 = [[UILabel alloc]init];
    [_profitView addSubview:label2];
    [label2 lt_setLeft:X(12) to:nil save:YES];
    //    [label2 lt_setTop:X(92) to:nil save:YES];
    [label2 lt_setBottom:X(12) to:nil save:YES];
    [label2 lt_setHeight:X(25) save:YES];
    label2.textColor = [UIColor colorWithHexString:@"050505"];
    label2.font = [UIFont systemFontOfSize:12];
    label2.text = @"预计到期收益";
    _userInputProfit = [[UILabel alloc]init];
    [_profitView addSubview:_userInputProfit];
    [_userInputProfit lt_setRight:X(12) to:nil save:YES];
    [_userInputProfit lt_YcenterTo:label2 save:YES];
    _userInputProfit.textAlignment = NSTextAlignmentRight;
    _userInputProfit.textColor = [UIColor colorWithHexString:@"050505"];
    _userInputProfit.font = [UIFont systemFontOfSize:12];
}
#pragma mark- 项目资料
- (void)creatdetailView{
    float top = Y(16);
    float left = X(20);
    float margin = X(4);
    float width = (SCREENWIDTH - left * 2 - margin * 2) / 3;
    float height = 90;
    _detailView = [[UIView alloc]init];
    UIView *bg1 = [[UIView alloc]init];
    [_detailView addSubview:bg1];
    [bg1 lt_Top:SunEdgesMake(0, 0, 0, 1) to:nil save:YES];
    bg1.backgroundColor = BACKGROUND;
    UIView *line1 = [[UIView alloc]init];
    [_detailView addSubview:line1];
    [line1 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:bg1 save:YES];
    line1.backgroundColor = LINE;
    
    UILabel *img1 = [[UILabel alloc]init];
    [_detailView addSubview:img1];
    [img1 lt_setSize:CGSizeMake(X(3),X(15)) save:YES];
    [img1 lt_setTop:Y(13) to:line1 save:YES];
    [img1 lt_setLeft:0 to:nil save:YES];
    img1.backgroundColor = COLOR_MAIN;
    
    UILabel *l1 = [[UILabel alloc]init];
    [_detailView addSubview:l1];
    [l1 lt_YcenterTo:img1 save:YES];
    [l1 lt_setLeft:X(6) to:img1 save:YES];
    l1.textColor = BLACK;
    l1.font = [UIFont systemFontOfSize:14];
    l1.text = @"项目描述";
    UIView *line2 = [Sun copyWithView:line1];
    [_detailView addSubview:line2];
    [line2 lt_Top:SunEdgesMake(Y(13), 0, 0, 0.5) to:img1 save:YES];
    UILabel *content1 = [[UILabel alloc]init];
    [_detailView addSubview:content1];
    [content1 lt_setLeft:X(20) to:nil Right:X(20) to:nil save:YES];
    [content1 lt_setTop:Y(15) to:line2 save:YES];
    content1.numberOfLines = 0;
    content1.preferredMaxLayoutWidth = SCREENWIDTH - X(40);
    content1.textColor = [UIColor colorWithHexString:@"6E6E6E"];
    content1.font = [UIFont systemFontOfSize:15];
    NSString *string = [_borrowDetail objectForKey:@"itemInfo"];
    string = [Sun htmlFormat:string];
    string  = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    content1.text = string;
    //    [XSP labelSetLineHeight:4 To:content1];
    UIView *temp = content1;
    string = [_borrowDetail objectForKey:@"itemImgs"];
    if (string.length > 0) {
        NSArray *arr = [string componentsSeparatedByString:@","];
        _imgNum1 = (int)arr.count;
        for (int i=0; i<arr.count; i++) {
            NSString *img = arr[i];
            if (![img containsString:@"http"]) {
                img = [KIMG stringByAppendingString:img];
            }
            [_imgArr addObject:img];
            UIImageView * pic = [[UIImageView alloc]init];
            [_detailView addSubview:pic];
            pic.userInteractionEnabled = YES;
            pic.tag = 1000 + i;
            UITapGestureRecognizer *imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImgs:)];
            [pic addGestureRecognizer:imgTap];
            [pic lt_setSize:CGSizeMake(width, height) save:YES];
            [pic lt_setTop:top + (height + margin) * (i / 3) to:content1 save:YES];
            [pic lt_setLeft:left + (width + margin) * (i % 3) to:nil save:YES];
            [pic sd_setImageWithURL:[NSURL URLWithString:img]];
            pic.contentMode = UIViewContentModeScaleAspectFill;
            pic.layer.borderWidth = 0.5;
            pic.layer.borderColor = GRAY.CGColor;
            pic.clipsToBounds = YES;
            temp = pic;
        }
    }else {
        temp = [[UIView alloc]init];
        [_detailView addSubview:temp];
        [temp lt_Top:SunEdgesMake(Y(15), 0, 0, Y(5)) to:content1 save:YES];
    }
    UIView *line3 = [Sun copyWithView:line1];
    [_detailView addSubview:line3];
    [line3 lt_Top:SunEdgesMake(Y(15), 0, 0, 0.5) to:temp save:YES];
    //
    UIView *bg10 = [[UIView alloc]init];
    [_detailView addSubview:bg10];
    [bg10 lt_Top:SunEdgesMake(0, 0, 0, 1) to:line3 save:YES];
    bg10.backgroundColor = BACKGROUND;
    UIView *line10 = [[UIView alloc]init];
    [_detailView addSubview:line10];
    [line10 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:bg10 save:YES];
    line10.backgroundColor = LINE;
    UILabel *img10 = [[UILabel alloc]init];
    [_detailView addSubview:img10];
    [img10 lt_setSize:CGSizeMake(X(3), X(15)) save:YES];
    [img10 lt_setTop:Y(13) to:line10 save:YES];
    [img10 lt_setLeft:0 to:nil save:YES];
    img10.backgroundColor = COLOR_MAIN;
    UILabel *l10 = [[UILabel alloc]init];
    [_detailView addSubview:l10];
    [l10 lt_YcenterTo:img10 save:YES];
    [l10 lt_setLeft:X(6) to:img10 save:YES];
    l10.textColor = BLACK;
    l10.font = [UIFont systemFontOfSize:14];
    l10.text = @"借款人介绍";
    UIView *line11 = [Sun copyWithView:line1];
    [_detailView addSubview:line11];
    [line11 lt_Top:SunEdgesMake(Y(13), 0, 0, 0.5) to:img10 save:YES];
    UILabel *content10 = [[UILabel alloc]init];
    [_detailView addSubview:content10];
    [content10 lt_setLeft:X(20) to:nil Right:X(20) to:nil save:YES];
    [content10 lt_setTop:Y(15) to:line11 save:YES];
    content10.numberOfLines = 0;
    content10.preferredMaxLayoutWidth = SCREENWIDTH - X(40);
    content10.textColor = [UIColor colorWithHexString:@"6E6E6E"];
    content10.font = [UIFont systemFontOfSize:15];
    NSString *string10 = [_borrowDetail objectForKey:@"borrowerInfo"];
    string10 = [Sun htmlFormat:string10];
    string10  = [string10 stringByReplacingOccurrencesOfString:@" " withString:@""];
    content10.text = string10;
    //    [XSP labelSetLineHeight:4 To:content10];
    temp = [[UIView alloc]init];
    [_detailView addSubview:temp];
    [temp lt_Top:SunEdgesMake(Y(15), 0, 0, Y(5)) to:content10 save:YES];
    UIView *line12 = [Sun copyWithView:line1];
    [_detailView addSubview:line12];
    [line12 lt_Top:SunEdgesMake(Y(15), 0, 0, 0.5) to:temp save:YES];
    UIView *sub = line12;
    //
    if ([[_borrowDetail objectForKey:@"repaymentInfo"] length] > 0) {
        UIView *bg11 = [[UIView alloc]init];
        [_detailView addSubview:bg11];
        [bg11 lt_Top:SunEdgesMake(0, 0, 0, 1) to:line12 save:YES];
        bg11.backgroundColor = BACKGROUND;
        UIView *line13 = [[UIView alloc]init];
        [_detailView addSubview:line13];
        [line13 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:bg11 save:YES];
        line13.backgroundColor = LINE;
        UILabel *img11 = [[UILabel alloc]init];
        [_detailView addSubview:img11];
        [img11 lt_setSize:CGSizeMake(X(3), X(15)) save:YES];
        [img11 lt_setTop:Y(13) to:line13 save:YES];
        [img11 lt_setLeft:0 to:nil save:YES];
        img11.backgroundColor = COLOR_MAIN;
        UILabel *l11 = [[UILabel alloc]init];
        [_detailView addSubview:l11];
        [l11 lt_YcenterTo:img11 save:YES];
        [l11 lt_setLeft:X(6) to:img11 save:YES];
        l11.textColor = BLACK;
        l11.font = [UIFont systemFontOfSize:14];
        l11.text = @"还款来源";
        UIView *line14 = [Sun copyWithView:line1];
        [_detailView addSubview:line14];
        [line14 lt_Top:SunEdgesMake(Y(13), 0, 0, 0.5) to:img11 save:YES];
        UILabel *content11 = [[UILabel alloc]init];
        [_detailView addSubview:content11];
        [content11 lt_setLeft:X(20) to:nil Right:X(20) to:nil save:YES];
        [content11 lt_setTop:Y(15) to:line14 save:YES];
        content11.numberOfLines = 0;
        content11.preferredMaxLayoutWidth = SCREENWIDTH - X(40);
        content11.textColor = [UIColor colorWithHexString:@"6E6E6E"];
        content11.font = [UIFont systemFontOfSize:15];
        NSString *string11 = [_borrowDetail objectForKey:@"repaymentInfo"];
        string11 = [Sun htmlFormat:string11];
        string11  = [string11 stringByReplacingOccurrencesOfString:@" " withString:@""];
        content11.text = string11;
        temp = [[UIView alloc]init];
        [_detailView addSubview:temp];
        [temp lt_Top:SunEdgesMake(Y(15), 0, 0, Y(5)) to:content11 save:YES];
        UIView *line15 = [Sun copyWithView:line1];
        [_detailView addSubview:line15];
        [line15 lt_Top:SunEdgesMake(Y(15), 0, 0, 0.5) to:temp save:YES];
        sub = line15;
    }
    UIView *bg2 = [Sun copyWithView:bg1];
    [_detailView addSubview:bg2];
    [bg2 lt_Top:SunEdgesMake(0, 0, 0, 1) to:sub save:YES];
    UIView *line4 = [Sun copyWithView:line1];
    [_detailView addSubview:line4];
    [line4 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:bg2 save:YES];
    
    UILabel *img2 = [Sun copyWithView:img1];
    [_detailView addSubview:img2];
    [img2 lt_setSize:CGSizeMake(X(3), X(15)) save:YES];
    [img2 lt_setTop:Y(13) to:line4 save:YES];
    [img2 lt_setLeft:0 to:nil save:YES];
    img2.backgroundColor = COLOR_MAIN;
    
    UILabel *l2 = [Sun copyWithView:l1];
    [_detailView addSubview:l2];
    [l2 lt_YcenterTo:img2 save:YES];
    [l2 lt_setLeft:X(6) to:img2 save:YES];
    l2.text = @"风控信息";
    UIView *line5 = [Sun copyWithView:line1];
    [_detailView addSubview:line5];
    [line5 lt_Top:SunEdgesMake(Y(13), 0, 0, 0.5) to:l2 save:YES];
    UILabel *content2 = [Sun copyWithView:content1];
    [_detailView addSubview:content2];
    [content2 lt_setLeft:X(20) to:nil Right:X(20) to:nil save:YES];
    [content2 lt_setTop:Y(15) to:line5 save:YES];
    string = [_borrowDetail objectForKey:@"riskInfo"];
    string = [Sun htmlFormat:string];
    string  = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    content2.text = string;
    temp = content2;
    string = [_borrowDetail objectForKey:@"riskImgs"];
    if (string.length > 0) {
        NSArray *arr = [string componentsSeparatedByString:@","];
        _imgNum2 = (int)arr.count;
        for (int i=0; i<arr.count; i++) {
            NSString *img = arr[i];
            if (![img containsString:@"http"]) {
                img = [KIMG stringByAppendingString:img];
            }
            [_imgArr addObject:img];
            UIImageView * pic = [[UIImageView alloc]init];
            [_detailView addSubview:pic];
            pic.userInteractionEnabled = YES;
            pic.tag = 2000 + i;
            UITapGestureRecognizer *imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImgs:)];
            [pic addGestureRecognizer:imgTap];
            [pic lt_setSize:CGSizeMake(width, height) save:YES];
            [pic lt_setTop:top + (height + margin) * (i / 3) to:content2 save:YES];
            [pic lt_setLeft:left + (width + margin) * (i % 3) to:nil save:YES];
            [pic sd_setImageWithURL:[NSURL URLWithString:img]];
            pic.contentMode = UIViewContentModeScaleAspectFill;
            pic.layer.borderWidth = 0.5;
            pic.layer.borderColor = GRAY.CGColor;
            pic.clipsToBounds = YES;
            temp = pic;
        }
    }else {
        temp = [[UIView alloc]init];
        [_detailView addSubview:temp];
        [temp lt_Top:SunEdgesMake(Y(15), 0, 0, Y(5)) to:content2 save:YES];
    }
    UIView *line6 = [Sun copyWithView:line1];
    [_detailView addSubview:line6];
    [line6 lt_Top:SunEdgesMake(Y(15), 0, 0, 0.5) to:temp save:YES];
    UIView *bg3 = [Sun copyWithView:bg1];
    [_detailView addSubview:bg3];
    [bg3 lt_Top:SunEdgesMake(0, 0, 0, 1) to:line6 save:YES];
    UIView *line7 = [Sun copyWithView:line1];
    [_detailView addSubview:line7];
    [line7 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:bg3 save:YES];
    UILabel *img3 = [Sun copyWithView:img1];
    [_detailView addSubview:img3];
    [img3 lt_setSize:CGSizeMake(X(3), X(15)) save:YES];
    [img3 lt_setTop:Y(13) to:line7 save:YES];
    [img3 lt_setLeft:0 to:nil save:YES];
    img3.backgroundColor = COLOR_MAIN;
    UILabel *l3 = [Sun copyWithView:l1];
    [_detailView addSubview:l3];
    [l3 lt_YcenterTo:img3 save:YES];
    [l3 lt_setLeft:X(6) to:img3 save:YES];
    l3.text = @"项目资料";
    UIView *line8 = [Sun copyWithView:line1];
    [_detailView addSubview:line8];
    [line8 lt_Top:SunEdgesMake(Y(13), 0, 0, 0.5) to:l3 save:YES];
    temp = line8;
    string = [_borrowDetail objectForKey:@"projectImgs"];
    if (string.length > 0) {
        NSArray *arr = [string componentsSeparatedByString:@","];
        for (int i=0; i<arr.count; i++) {
            NSString *img = arr[i];
            if (![img containsString:@"http"]) {
                img = [KIMG stringByAppendingString:img];
            }
            [_imgArr addObject:img];
            UIImageView * pic = [[UIImageView alloc]init];
            [_detailView addSubview:pic];
            pic.userInteractionEnabled = YES;
            pic.tag = 3000 + i;
            UITapGestureRecognizer *imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImgs:)];
            [pic addGestureRecognizer:imgTap];
            [pic lt_setSize:CGSizeMake(width, height) save:YES];
            [pic lt_setTop:top + (height + margin) * (i / 3) to:line8 save:YES];
            [pic lt_setLeft:left + (width + margin) * (i % 3) to:nil save:YES];
            [pic sd_setImageWithURL:[NSURL URLWithString:img]];
            pic.contentMode = UIViewContentModeScaleAspectFill;
            pic.layer.borderWidth = 0.5;
            pic.layer.borderColor = GRAY.CGColor;
            pic.clipsToBounds = YES;
            temp = pic;
        }
    }else {
        temp = [[UIView alloc]init];
        [_detailView addSubview:temp];
        [temp lt_Top:SunEdgesMake(Y(15), 0, 0, Y(5)) to:line8 save:YES];
    }
    [_detailView layoutIfNeeded];
    _detailView.frame = CGRectMake(0, 0, 0, temp.frame.origin.y + temp.frame.size.height + Y(15));
    [[Sun shareSun]hideAll];
    //    int section = _sessionArr.count == 3 ? 2 : 1;
    //    [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    //    [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
#pragma mark- createSafeView
- (void)createSafeView {
    NSString *urlStr = [NSString stringWithFormat:@"%@/guide_about.html?source=ios",KWAP];
    //    [self.navigationController pushViewController:vc animated:YES];
    self.safeView = [[UIWebView alloc]init];
    self.safeView.frame = CGRectMake(0, 0, 0, SCREENHEIGHT - 64 - X(80));
    [self.safeView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    self.safeView.scalesPageToFit = YES;
    [self.safeView layoutIfNeeded];
    [[Sun shareSun]hideAll];
    
    int section = _sessionArr.count == 2 ? 1 : 0;
    [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
#pragma mark- 上方区域
- (void)creatUI{
    // 上方区域
    UIView *bgView = [[UIView alloc]init];
    [bgView setBackgroundColor:[UIColor colorWithHexString:@"F6F6F6"]];
    [bgView setFrame:CGRectMake(0, 0, SCREENWIDTH, X(261))];
    [self.view addSubview:bgView];
    
    UIView *mainView = [[UIView alloc]init];
    [mainView setBackgroundColor:COLOR_MAIN];
    [mainView setFrame:CGRectMake(0, 0, SCREENWIDTH, X(156))];
    [bgView addSubview:mainView];
    
    
    UIImageView *precentBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pd_bg_circle"]];
    [precentBg setFrame:CGRectMake(SCREENWIDTH - X(131), 0, X(131), X(156))];
    [bgView addSubview:precentBg];
    
    UILabel *label1 = [[UILabel alloc]init];
    [bgView addSubview:label1];
    [label1 setFrame:CGRectMake(X(114), X(17), 100, X(13))];
    label1.font = [UIFont systemFontOfSize:10];
    label1.textColor = WHITE;
    label1.text = @"预期年化收益率";
    // 年化
    _basePercent = [[UILabel alloc]init];
    [bgView addSubview:_basePercent];
    _basePercent.font =  [UIFont fontWithName:@"DINAlternate-Bold" size:42];
    [_basePercent lt_setTop:X(6) to:label1 save:YES];
    [_basePercent lt_XcenterTo:nil save:YES];
    _basePercent.textColor = WHITE;
    [_basePercent setMarkup:@"<font size=\"42\" weight=\"0.3\">0.00</font><font size=\"18\">%</font>"];
    
    // 进度
    _progressView = [[UIView alloc]init];
    [bgView addSubview:_progressView];
    //    [_progressView lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    //    [_progressView lt_setTop:X(10) to:_basePercent save:YES];
    //    [_progressView lt_setHeight:3 save:YES];
    [_progressView setFrame:CGRectMake(0, X(91), SCREENWIDTH, 3)];
    _progressView.backgroundColor = [UIColor colorWithHexString:@"FFFFFF" alpha:0.2];
    UIView *progressBox = [[UIView alloc]init];
    [_progressView addSubview:progressBox];
    progressBox.backgroundColor = [HXColor colorWithHexString:@"FFFFFF"];
    progressBox.tag = 1;
    [progressBox lt_Left:SunEdgesMake(0, 0, 0, 0) to:nil save:YES];
    self.progressImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_percents"]];
    //    self.progressImage.backgroundColor = [UIColor blueColor];
    [self.progressImage setFrame:CGRectMake(X(10), X(81), 26,X(20))];
    [bgView addSubview:self.progressImage];
    _progressLable = [[UILabel alloc]init];
    [self.progressImage addSubview:_progressLable];
    [_progressLable setFrame:CGRectMake(0, X(5), 32, 14)];
    _progressLable.font = [UIFont systemFontOfSize:10];
    _progressLable.layer.cornerRadius = 7;
    _progressLable.layer.masksToBounds = YES;
    _progressLable.textColor = COLOR_MAIN;
    _progressLable.text = @"0%";
    _progressLable.backgroundColor = WHITE;
    _progressLable.textAlignment = NSTextAlignmentCenter;
    
    // 剩余可投
    UILabel *leaveWord = [[UILabel alloc]init];
    [mainView addSubview:leaveWord];
    [leaveWord lt_setBottom:X(6) to:nil save:YES];
    [leaveWord lt_setLeft:X(35) to:nil save:YES];
    leaveWord.textColor = [UIColor colorWithHexString:@"FFFFFF" alpha:0.69];
    leaveWord.font =  [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    leaveWord.text = @"剩余可借(元)";
    _leave = [[UILabel alloc]init];
    [mainView addSubview:_leave];
    [_leave lt_setBottom:X(5) to:leaveWord save:YES];
    [_leave lt_XcenterTo:leaveWord save:YES];
    _leave.textColor = WHITE;
    _leave.font = [UIFont fontWithName:@"DINAlternate-Bold" size:14];
    _leave.text = @"0";
    
    // 融资金额
    UILabel *totalWord = [[UILabel alloc]init];
    [mainView addSubview:totalWord];
    [totalWord lt_setBottom:X(6) to:nil save:YES];
    [totalWord lt_XcenterTo:nil save:YES];
    totalWord.textColor = [UIColor colorWithHexString:@"FFFFFF" alpha:0.69];
    totalWord.font =  [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    totalWord.text = @"融资总额(元)";
    _total = [[UILabel alloc]init];
    [mainView addSubview:_total];
    [_total lt_setBottom:X(5) to:totalWord save:YES];
    [_total lt_XcenterTo:totalWord save:YES];
    _total.textColor = WHITE;
    _total.font =  [UIFont fontWithName:@"DINAlternate-Bold" size:14];
    _total.text = @"0";
    
    // 起投金额
    UILabel *label3 = [[UILabel alloc]init];
    [mainView addSubview:label3];
    [label3 lt_YcenterTo:totalWord save:YES];
    [label3 lt_setRight:X(35) to:nil save:YES];
    label3.font =  [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    label3.textColor = [UIColor colorWithHexString:@"FFFFFF" alpha:0.69];
    label3.text = @"起借金额(元)";
    _days = [[UILabel alloc]init];
    [mainView addSubview:_days];
    [_days lt_XcenterTo:label3 save:YES];
    [_days lt_YcenterTo:_total save:YES];
    _days.textColor = WHITE;
    _days.font = [UIFont fontWithName:@"DINAlternate-Bold" size:14];
    _days.text = @"0";
#pragma mark- 蓝条区域
    UIView *blueLine = [[UIView alloc]init];
    [bgView addSubview:blueLine];
    [blueLine setBackgroundColor:[UIColor colorWithHexString:@"D8E9FE"]];
    [blueLine lt_setSize:CGSizeMake(SCREENWIDTH, X(0)) save:YES];
    blueLine.clipsToBounds = YES;
    //    [blueLine lt_setSize:CGSizeMake(SCREENWIDTH, X(25)) save:YES];
    [blueLine lt_setTop:X(8) to:mainView save:YES];
    [blueLine lt_XcenterTo:nil save:YES];
    UILabel *startWord = [[UILabel alloc]init];
    [blueLine addSubview:startWord];
    startWord.text = @"募集期";
    startWord.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    startWord.textColor = BLACK;
    startWord.textAlignment = NSTextAlignmentCenter;
    [startWord lt_setSize:CGSizeMake(SCREENWIDTH/3-2, X(25)) save:YES];
    [startWord lt_setLeft:0 to:nil save:YES];
    [startWord lt_YcenterTo:nil save:YES];
    UIImageView *line1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_line"]];
    [blueLine addSubview:line1];
    [line1 lt_setSize:CGSizeMake(X(1), X(23)) save:nil];
    [line1 lt_YcenterTo:nil save:YES];
    [line1 lt_setLeft:SCREENWIDTH/3 to:nil save:YES];
    self.timeWord = [[UILabel alloc]init];
    [blueLine addSubview:self.timeWord];
    self.timeWord.text = @"锁定期**天";
    self.timeWord.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    self.timeWord.textColor = BLACK;
    self.timeWord.textAlignment = NSTextAlignmentCenter;
    [self.timeWord lt_setSize:CGSizeMake(SCREENWIDTH/3-2, X(25)) save:YES];
    [self.timeWord lt_XcenterTo:nil save:YES];
    [self.timeWord lt_YcenterTo:nil save:YES];
    UIImageView *line2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_line"]];
    [blueLine addSubview:line2];
    [line2 lt_setSize:CGSizeMake(X(1), X(23)) save:nil];
    [line2 lt_YcenterTo:nil save:YES];
    [line2 lt_setRight:SCREENWIDTH/3 to:nil save:YES];
    self.wayWord = [[UILabel alloc]init];
    [blueLine addSubview:self.wayWord];
    self.wayWord.text = @"到期一次还本付息";
    self.wayWord.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    self.wayWord.textColor = BLACK;
    self.wayWord.textAlignment = NSTextAlignmentCenter;
    [self.wayWord lt_setSize:CGSizeMake(SCREENWIDTH/3-2, X(25)) save:YES];
    [self.wayWord lt_setRight:0 to:nil save:YES];
    [self.wayWord lt_YcenterTo:nil save:YES];
#pragma mark- 时间线
    UIView *timeView = [[UIView alloc]init];
    [bgView addSubview:timeView];
    [timeView setBackgroundColor:WHITE];
    [timeView lt_setSize:CGSizeMake(SCREENWIDTH, X(84)) save:YES];
    [timeView lt_XcenterTo:nil save:YES];
    [timeView lt_setTop:0 to:blueLine save:YES];
    UIImageView *timeLine = [[UIImageView alloc]init];
    [timeView addSubview:timeLine];
    [timeLine setImage:[UIImage imageNamed:@"ic_invest_detail"]];
    [timeLine lt_setSize:CGSizeMake(X(304), X(24)) save:YES];
    [timeLine lt_XcenterTo:nil save:YES];
    [timeLine lt_setTop:X(9) to:nil save:YES];
    
    UILabel *beginWord = [[UILabel alloc]init];
    [timeView addSubview:beginWord];
    beginWord.text = @"开始募集";
    beginWord.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    beginWord.textColor = COLOR_TITLE_MAIN;
    [beginWord lt_setTop:X(8) to:timeLine save:YES];
    [beginWord lt_setLeft:X(30) to:nil save:YES];
    self.beginTime = [[UILabel alloc]init];
    [timeView addSubview:self.beginTime];
    self.beginTime.text = @"2018/04/16";
    self.beginTime.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    self.beginTime.textColor = COLOR_TITLE_MAIN;
    [self.beginTime lt_setTop:X(3) to:beginWord save:YES];
    [self.beginTime lt_XcenterTo:beginWord save:YES];
    
    UILabel *fullWord = [[UILabel alloc]init];
    [timeView addSubview:fullWord];
    fullWord.text = @"满标起息";
    fullWord.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    fullWord.textColor = COLOR_TITLE_MAIN;
    [fullWord lt_setTop:X(8) to:timeLine save:YES];
    [fullWord lt_setLeft:X(50) to:beginWord save:YES];
    self.fullTime = [[UILabel alloc]init];
    [timeView addSubview:self.fullTime];
    self.fullTime.text = @"募集完成日期";
    self.fullTime.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    self.fullTime.textColor = COLOR_TITLE_MAIN;
    [self.fullTime lt_setTop:X(3) to:fullWord save:YES];
    [self.fullTime lt_XcenterTo:fullWord save:YES];
    
    UILabel *closeTimeTip = [[UILabel alloc]init];
    [timeView addSubview:closeTimeTip];
    closeTimeTip.text = @"锁定期";
    closeTimeTip.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    closeTimeTip.textColor = COLOR_TITLE_MAIN;
    [closeTimeTip lt_setTop:X(8) to:timeLine save:YES];
    [closeTimeTip lt_setLeft:X(57) to:fullWord save:YES];
    self.closeTime = [[UILabel alloc]init];
    [timeView addSubview:self.closeTime];
    self.closeTime.text = @"2018/04/16";
    self.closeTime.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    self.closeTime.textColor = COLOR_TITLE_MAIN;
    [self.closeTime lt_setTop:X(3) to:closeTimeTip save:YES];
    [self.closeTime lt_XcenterTo:closeTimeTip save:YES];
    
    
    UILabel *repayWord = [[UILabel alloc]init];
    [timeView addSubview:repayWord];
    repayWord.text = @"到期还款";
    repayWord.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    repayWord.textColor = COLOR_TITLE_MAIN;
    [repayWord lt_setTop:X(8) to:timeLine save:YES];
    [repayWord lt_setRight:X(28) to:nil save:YES];
    self.repayTime = [[UILabel alloc]init];
    [timeView addSubview:self.repayTime];
    self.repayTime.text = @"2018/04/16";
    self.repayTime.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    self.repayTime.textColor = COLOR_TITLE_MAIN;
    [self.repayTime lt_setTop:X(3) to:repayWord save:YES];
    [self.repayTime lt_XcenterTo:repayWord save:YES];
    
    _tb = [[UITableView alloc]init];
    [self.view addSubview:_tb];
    [_tb lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    [_tb lt_setTop:0 to:nil Bottom:X(42) to:nil save:YES];
    _tb.backgroundColor = BACKGROUND;
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.showsVerticalScrollIndicator = NO;
    _tb.tableHeaderView = bgView;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _pay = [[UIButton alloc]init];
    [self.view addSubview:_pay];
    [_pay setBackgroundImage:[Sun imageWithColorFrom:[UIColor colorWithHexString:@"FB8165"] To:[UIColor colorWithHexString:@"F96C5F"] Frame:CGRectMake(0, 0, SCREENWIDTH - X(42), X(42)) Direction:SunDirectionRight] forState:UIControlStateNormal];
    [_pay addTarget:self action:@selector(openInvestmentView) forControlEvents:UIControlEventTouchUpInside];
    [_pay setTitle:@"立即出借" forState:UIControlStateNormal];
    [_pay setTitleColor:WHITE forState:UIControlStateNormal];
    _pay.titleLabel.font = [UIFont systemFontOfSize:18];
    [self requestToBorrowDetail];
    
    self.calculateBtn = [[UIButton alloc]init];
    [self.view addSubview:self.calculateBtn];
    [self.calculateBtn setBackgroundImage:[UIImage imageNamed:@"pd_calculate"] forState:UIControlStateNormal];
    [self.calculateBtn addTarget:self action:@selector(calculateAction) forControlEvents:UIControlEventTouchUpInside];
    [_pay setFrame:CGRectMake(X(42),  self.view.frame.size.height - X(42)-64-SafeAreaBottomHeight*1.7, SCREENWIDTH - X(42), X(42))];
    [self.calculateBtn setFrame:CGRectMake(0,  self.view.frame.size.height - X(47)-64-SafeAreaBottomHeight*1.7, X(48), X(47))];
    
}
- (void)calculateAction {
    [_userInput becomeFirstResponder];
}
#pragma mark function
- (void)showImgs:(UITapGestureRecognizer *)tap{
    SunImgShows *vc = [[SunImgShows alloc]init];
    vc.imgs = _imgArr;
    int index = 0;
    if (tap.view.tag / 1000 == 1) {
        index = (int)tap.view.tag - 1000;
    }
    if (tap.view.tag / 1000 == 2) {
        index = (int)tap.view.tag - 2000 + _imgNum1;
    }
    if (tap.view.tag / 1000 == 3) {
        index = (int)tap.view.tag - 3000 + _imgNum1 + _imgNum2;
    }
    vc.currentIndex = index;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setProgress:(float)progress{
    _progressLable.text = [NSString stringWithFormat:@"%.0f%%",progress * 100];
    UIView *progressBox = [_progressView viewWithTag:1];
    [progressBox lt_removeAllCons];
    [UIView animateWithDuration:0.5 animations:^{
        [progressBox lt_Left:SunEdgesMake(0, 0, 0, progressBox.superview.bounds.size.width * progress) to:nil save:YES];
        if (progress >= 0.95) {
            [self.progressImage setFrame:CGRectMake(SCREENWIDTH - 32, X(81), 26, 20)];
        }else if (progress <= 0.05){
            [self.progressImage setFrame:CGRectMake(0, X(81), 26, 20)];
        }else {
            [self.progressImage setFrame:CGRectMake(progressBox.superview.bounds.size.width * progress - X(10), X(81), 26, 20)];
        }
        [self.view layoutIfNeeded];
    }];
}

- (void)updateHead{
    if (!_borrowInfo) {
        return;
    }
    _titleLabel.text = [_borrowInfo objectForKey:@"name"];
    [_leave setMarkup:[NSString stringWithFormat:@"<font size=\"14\" >%@</font>",[Sun formatStringToMoney:[_borrowInfo objectForKey:@"leaveMoney"]]]];
    _total.text = [NSString stringWithFormat:@"%@",[Sun formatStringToMoney:[_borrowInfo objectForKey:@"totalMoney"]]];
    _days.text = [_borrowInfo objectForKey:@"limitMoney"];
    
    
    self.wayWord.text = [_borrowInfo objectForKey:@"repayType"];
    [self setProgress:[[_borrowInfo objectForKey:@"progress"]doubleValue]];
    //    [_borrowInfo objectForKey:@"schedule"]
    self.beginTime.text = [[_borrowInfo objectForKey:@"schedule"] substringToIndex:10];
    self.closeTime.text = [NSString stringWithFormat:@"%@天",[_borrowInfo objectForKey:@"days"]];
    
    self.repayTime.text = [_borrowInfo objectForKey:@"repayType"];
    if ([[_borrowInfo objectForKey:@"status"] intValue]==0) {
        [_pay setTitle:@"已满标请选择其它项目" forState:UIControlStateNormal];
        self.fullTime.text =[_borrowInfo objectForKey:@"fullDay"];
        _pay.enabled = NO;
        self.calculateBtn.hidden = YES;
        [_pay setFrame:CGRectMake(0, self.view.frame.size.height - X(42)-SafeAreaBottomHeight, SCREENWIDTH, X(42))];
    }
    if ([[_borrowInfo objectForKey:@"type"]intValue] == 1 && [[_borrowInfo objectForKey:@"isNewUser"]intValue] == 0) {
        [_pay setTitle:@"只限新手用户" forState:UIControlStateNormal];
        _pay.enabled = NO;
    }
    if ([[_borrowInfo objectForKey:@"aprAdd"] doubleValue] > 0) {
        [_basePercent setMarkup:[NSString stringWithFormat:@"<font size=\"42\" >%.2f</font><font size=\"18\">%%+%.2f%%</font>",[[_borrowInfo objectForKey:@"aprBase"]doubleValue],[[_borrowInfo objectForKey:@"aprAdd"] doubleValue]]];
    }else {
        [_basePercent setMarkup:[NSString stringWithFormat:@"<font size=\"42\" >%.2f</font><font size=\"18\">%%</font>",[[_borrowInfo objectForKey:@"aprBase"]doubleValue]]];
    }
}

#pragma mark calculate
- (float)getProfit:(float)money{
    float count = [[_borrowInfo objectForKey:@"days"]intValue];
    if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
        count = count/30;
    }
    float x = ([[_borrowInfo objectForKey:@"aprBase"]doubleValue] + [[_borrowInfo objectForKey:@"aprAdd"]doubleValue]) / 100.0;
    float value = money * x * count / 12.0;
    float result = floor(value * 100) / 100.0;
    return result;
}

- (int)getTotalMoney:(float)profit{
    float count = [[_borrowInfo objectForKey:@"days"]intValue];
    if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
        count = count/30;
    }
    float x = ([[_borrowInfo objectForKey:@"aprBase"]doubleValue] + [[_borrowInfo objectForKey:@"aprAdd"]doubleValue]) / 100.0;
    float result = profit / x / count * 12.0;
    if (result - (int)result >= 0.5) {
        return (int)result + 1;
    }else {
        return (int)result;
    }
}

#pragma mark button


- (void)hideAlert{
    [_sunAlert hideAll];
}

- (void)back{
    if (self.isWeb) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)share{
    
}




- (void)changeDetail:(UIButton *)btn{
    if (_selectNum == (int)btn.tag - 1) {
        if (!_detailView) {
            [[Sun shareSun]showActivity:self];
            return;
        }
    }
    _selectNum = (int)btn.tag - 1;
    if (_selectNum == 1) {
        [self createSafeView];
        if (!self.safeView) {
            [[Sun shareSun]showActivity:self];
            return;
        }
    }
    if (_selectNum == 2) {
        [self requestToBorrowRecord];
        if (!_recordArr) {
            [[Sun shareSun]showActivity:self];
            return;
        }
    }
    
    int section = _sessionArr.count == 2 ? 1 : 0;
    [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

#pragma mark request

- (void)requestToBorrowRecord {
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWRECORD];
    NSDictionary *param = @{@"borrowId":self.borrowId};
    [HttpRequest postURL:url withParam:param success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        NSMutableArray *recArr = [NSMutableArray arrayWithArray:[requestDic objectForKey:@"list"]];
        _recordArr = [NSMutableArray arrayWithArray:[requestDic objectForKey:@"list"]];
        [self.specialRecord removeAllObjects];
        UIButton *btn = [self.view viewWithTag:3];
        [btn setTitle:[NSString stringWithFormat:@"投资记录(%lu人)",recArr.count] forState:UIControlStateNormal];
        for (NSDictionary *dic in recArr) {
            if ([[dic objectForKey:@"remark"] length]>0) {
                [self.specialRecord addObject:dic];
            }
        }
        if (_selectNum == 2) {
            int section = _sessionArr.count == 2 ? 1 : 0;
            [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
            [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)requestToBorrowDetail{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWDETAIL];
    [HttpRequest postURL:url withParam:@{@"borrowId":self.borrowId} success:^(id requestDic, NSString *msg) {
        _borrowDetail = requestDic;
        [self creatdetailView];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)requestToUser{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
        self.balanceMoney = [requestDic objectForKey:@"balanceMoney"];
        self.isRisk = [[requestDic objectForKey:@"isRisk"] integerValue];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)requestToBorrowInfo{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWINFO];
    NSDictionary *param = @{@"borrowId":self.borrowId};
    [HttpRequest postURL:url withParam:param success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        _borrowInfo = [requestDic objectForKey:@"borrowInfo"];
        NSMutableArray *allCards = [requestDic objectForKey:@"cards"];
        _sessionArr = [NSMutableArray arrayWithArray:@[@""]];
        if ([[_borrowInfo objectForKey:@"activity"]intValue] == 1) {
            [_sessionArr insertObject:@{@"title":[_borrowInfo objectForKey:@"activityName"],@"content":[_borrowInfo objectForKey:@"activityUrl"]} atIndex:0];
        }
        [self updateHead];
        self.canUseCard = [[_borrowInfo objectForKey:@"canUseCard"]intValue];
        //  1:只可以使用红包；   2:只可以使用加息卡；  3：红包加息卡都能使用   0:红包加息卡都不能使用
        if (self.canUseCard == 3) { // 红包加息卡都能使用
            _cards = allCards;
        }else if (self.canUseCard == 1){ // 只可以使用红包
            _cards = [[NSMutableArray alloc]init];
            for (NSDictionary *card in allCards) {
                if ([[card objectForKey:@"type"]intValue] == 1) {
                    [_cards addObject:card];
                }
            }
        }else if (self.canUseCard == 2){ //只可以使用加息卡
            _cards = [[NSMutableArray alloc]init];
            for (NSDictionary *card in allCards) {
                if ([[card objectForKey:@"type"]intValue] == 2) {
                    [_cards addObject:card];
                }
            }
        }
        if (!_profitView) {
            [self creatProfitView];
            
        }
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}




#pragma mark textField
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //    _ruler.rulerDeletate = self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < string.length) {
        NSString * str = [string substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [str rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    
    //    _ruler.rulerDeletate = nil;
    NSString *result = textField.text;
    if (string.length > 0) {
        result = [result stringByAppendingString:string];
    }else if(result.length > 0 && result.length > 0){
        result = [result substringToIndex:result.length - 1];
    }
    _userInputProfit.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:result.doubleValue]];
    //    [_ruler scrollToValue:result.doubleValue];
    _rulerValue = result.doubleValue;
    if (result.doubleValue > [[_borrowInfo objectForKey:@"leaveMoney"] doubleValue]) {
        NSString *s = [_borrowInfo objectForKey:@"leaveMoney"];
        s = [s isKindOfClass:[NSNumber class]]?[NSString stringWithFormat:@"%@",s]:s;
        result = s;
        textField.text = [NSString stringWithFormat:@"%d",result.intValue];
        _userInputProfit.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:result.doubleValue]];
        //        [_ruler scrollToValue:result.doubleValue];
        _rulerValue = result.doubleValue;
        if (string.length > 0) {
            res = NO;
        }
    }
    return res;
}

- (void)openInvestmentView{
    [_userInput resignFirstResponder];
    NSDictionary *dic = [Sun getUser:USER];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        SunLoginVC *vc = [[SunLoginVC alloc]init];
        vc.isWeb = NO;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }else if ([[dic objectForKey:@"status"] intValue] != 3) {
        [SunSupport shouldAlert:self code:UserOpen];
        return;
    }else if ([[dic objectForKey:@"status"] intValue] != 3) {
        [SunSupport shouldAlert:self code:UserBind];
        return;
    }else if ([[dic objectForKey:@"isActivate"] intValue] == 1) {
        [SunSupport shouldAlert:self code:UserActivate];
    }else if (self.isRisk == 0) {
        [self showRiskAlert];
    }else{
        XSPInvestmentVC *investVC = [[XSPInvestmentVC alloc]init];
        investVC.borrowInfo = _borrowInfo;
        investVC.cards = _cards;
        investVC.borrowId = self.borrowId;
        investVC.investString = _userInput.text;
        investVC.balanceMoney = self.balanceMoney;
        [self.navigationController pushViewController:investVC animated:YES];
    }
}
#pragma mark- risk
- (void)showRiskAlert{
    
    if (!self.riskView) {
        self.riskView = [[UIView alloc]init];
        [self.riskView lt_setSize:CGSizeMake(301, 164) save:YES];
        self.riskView.backgroundColor = WHITE;
        self.riskView.layer.cornerRadius = 27;
        self.riskView.clipsToBounds = YES;
        
        UIButton *cancel = [[UIButton alloc]init];
        [self.riskView addSubview:cancel];
        [cancel lt_setTop:Y(0) to:nil save:YES];
        [cancel lt_setLeft:X(0) to:nil save:YES];
        [cancel lt_setSize:CGSizeMake(0, 0) save:YES];
        [cancel lt_layoutView:cancel side:SunCenterX to:nil toSide:SunCenterX withSize:1 offset:X(-144) save:YES];
        [cancel setImage:[UIImage imageNamed:@"tool_cancel"] forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(hideAlert) forControlEvents:UIControlEventTouchUpInside];
        cancel.hidden = YES;
        
        UILabel *titleLabel = [[UILabel alloc]init];
        [self.riskView addSubview:titleLabel];
        titleLabel.text = @"风险测评";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor colorWithHexString:@"4A4A4A"];
        titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
        [titleLabel lt_XcenterTo:nil save:YES];
        [titleLabel lt_setTop:13 to:nil save:YES];
        
        UILabel *label = [[UILabel alloc] init];
        [self.riskView addSubview:label];
        [label lt_setSize:CGSizeMake(230, 55) save:YES];
        [label lt_XcenterTo:nil save:YES];
        [label lt_setTop:18 to:titleLabel save:YES];
        label.numberOfLines = 2;
        label.text = @"为保障您的权益，请在完成测评后进行投资。 ";
        label.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
        label.textColor = [UIColor colorWithHexString:@"6C6C6C"];
        
        UILabel *lineX = [[UILabel alloc]init];
        [self.riskView addSubview:lineX];
        [lineX lt_setSize:CGSizeMake(321, 1) save:YES];
        [lineX lt_XcenterTo:nil save:YES];
        [lineX lt_setTop:86 to:titleLabel save:YES];
        lineX.backgroundColor = [UIColor colorWithHexString:@"F7F7F7"];
        
        UILabel *lineY = [[UILabel alloc]init];
        [self.riskView addSubview:lineY];
        [lineY lt_setSize:CGSizeMake(1, 50) save:YES];
        [lineY lt_XcenterTo:nil save:YES];
        [lineY lt_setTop:0 to:lineX save:YES];
        lineY.backgroundColor = [UIColor colorWithHexString:@"F7F7F7"];
        
        
        UIButton *nextTime = [[UIButton alloc]init];
        [self.riskView addSubview:nextTime];
        [nextTime lt_setSize:CGSizeMake(145, 44) save:YES];
        [nextTime lt_setTop:2 to:lineX save:YES];
        [nextTime lt_setBottom:2 to:nil save:YES];
        [nextTime lt_setLeft:2 to:nil save:YES];
        [nextTime setTitleColor:[UIColor colorWithHexString:@"6C6C6C"] forState:UIControlStateNormal];
        [nextTime setTitle:@"暂不测评" forState:UIControlStateNormal];
        nextTime.titleLabel.font = [UIFont systemFontOfSize:14];
        [nextTime addTarget:self action:@selector(hideAlert) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *riskButton = [[UIButton alloc]init];
        [self.riskView addSubview:riskButton];
        [riskButton lt_setSize:CGSizeMake(145, 44) save:YES];
        [riskButton lt_setTop:2 to:lineX save:YES];
        [riskButton lt_setBottom:2 to:nil save:YES];
        [riskButton lt_setRight:2 to:nil save:YES];
        [riskButton setTitleColor:COLOR_MAIN forState:UIControlStateNormal];
        [riskButton setTitle:@"立即测评" forState:UIControlStateNormal];
        riskButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [riskButton addTarget:self action:@selector(goRiskView) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.sunAlert show:self.riskView to:self shouldReturn:YES];
}
- (void)goRiskView{
    [self hideAlert];
    SunWebVC *vc = [[SunWebVC alloc]init];
    NSString *str = @"CZfSDQzsuDayjfzE";
    NSString *str1 =@"12345678";
    NSString *token_id=[Utility encryptStr:[NSString stringWithFormat:@"%@",[Sun getUser:ACCESSID]]  Key:str1 IV:nil];
    NSString *md5Str=[Utility md5:[NSString stringWithFormat:@"source=ios&token_id=%@&%@",token_id,str]];
    vc.urlStr = [NSString stringWithFormat:@"%@/account/risk_evaluation?source=ios&token_id=%@&sign=%@",KWAP,token_id,md5Str];
    vc.titleName = @"风险测评";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y > 0) {
        _barLine.hidden = NO;
    }else{
        _barLine.hidden = YES;
    }
}

#pragma mark tableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if ((section == 1 && _sessionArr.count
         == 2) || (section == 0 && _sessionArr.count
                   == 1)) {
        return X(40);
    }else{
        return 0.01;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (_sessionArr.count == 2 ? section <= 2 : section <= 1) {
        return 2;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ((indexPath.section == 1 && _sessionArr.count == 2) || (indexPath.section == 0 && _sessionArr.count == 1)){
        if (_selectNum == 0) {
            return _detailView.bounds.size.height;
        }
        if (_selectNum == 1) {
            return self.safeView.bounds.size.height;
            
        }
        if (_selectNum == 2) {
            if (self.specialRecord.count == 0 && _recordArr.count == 0 && indexPath.row == 1){
                return SCREENHEIGHT - 64 - Y(100) - Y(62);
            }else {
                return 45;
            }
        }
    }
    return X(40);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sessionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ((_sessionArr.count == 2 && section == 1) || (_sessionArr.count == 1 && section == 0)){
        if (_selectNum == 2) {
            if (self.specialRecord.count == 0 && _recordArr.count == 0) {
                return 2;
            }else {
                return _recordArr.count + self.specialRecord.count + 1;
            }
        }else {
            return 1;
        }
    }else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    while (cell.subviews.count > 0) {
        for (int i=0; i<cell.subviews.count; i++) {
            [cell.subviews[i] removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0 && indexPath.section != 2) {
        cell.backgroundColor = [UIColor clearColor];
        UIImageView *backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pd_cellback"]];
        [cell setBackgroundView:backImage];
    }else{
        cell.backgroundColor = WHITE;
    }
    if (_sessionArr.count == 2 && indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *line = [[UILabel alloc]init];
        [cell addSubview:line];
        [line lt_setSize:CGSizeMake(X(3), X(15)) save:YES];
        [line lt_setLeft:0 to:nil save:YES];
        [line lt_YcenterTo:nil save:YES];
        line.backgroundColor = COLOR_MAIN;
        
        UILabel *label = [[UILabel alloc]init];
        [cell addSubview:label];
        [label lt_setLeft:X(18) to:nil save:YES];
        [label lt_YcenterTo:nil save:YES];
        label.textColor = COLOR_MAIN;
        label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        label.text = [NSString stringWithFormat:@"%@/%@",[_sessionArr[indexPath.section] objectForKey:@"title"],[_borrowInfo objectForKey:@"activityDesc"]];
        UIImageView *img = [[UIImageView alloc]init];
        [cell addSubview:img];
        [img lt_YcenterTo:nil save:YES];
        [img lt_setRight:X(12) to:nil save:YES];
        img.image = [UIImage imageNamed:@"second_right"];
    }//活动标题
    if ((_sessionArr.count == 2 && indexPath.section == 1) || (_sessionArr.count == 1 && indexPath.section == 0)) {
        if (_selectNum == 0) {
            [cell addSubview:_detailView];
            [_detailView lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
        }
        if (_selectNum == 1) {
            [cell addSubview:self.safeView];
            [self.safeView lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
        }
        if (_selectNum == 2) {
            if (self.specialRecord.count == 0 && _recordArr.count == 0) {
                UIView *view = [[UIView alloc]init];
                view.backgroundColor = BACKGROUND;
                [cell addSubview:view];
                [view lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
                UIImageView *img = [[UIImageView alloc]init];
                [view addSubview:img];
                [img lt_XcenterTo:nil save:YES];
                img.image = [UIImage imageNamed:@"third_order_blank"];
                UILabel *label = [[UILabel alloc]init];
                [view addSubview:label];
                [label lt_XcenterTo:nil save:YES];
                [label lt_setTop:Y(18) to:img save:YES];
                [label lt_layoutView:label side:SunCenterY to:nil toSide:SunCenterY withSize:1 offset:Y(80) save:YES];
                label.textColor = GRAY;
                label.font = [UIFont systemFontOfSize:19];
                label.text = @"暂无用户投资哦~";
            }else if (indexPath.row < self.specialRecord.count) {
                NSDictionary *data = self.specialRecord[indexPath.row];
                cell.backgroundColor = [UIColor colorWithHexString:@"FBFAFA"];
                UIImageView *bgImage = [[UIImageView alloc]init];
                [cell addSubview:bgImage];
                [bgImage setImage:[UIImage imageNamed:@"pd_record_special"]];
                [bgImage lt_setSize:CGSizeMake(X(345), X(29)) save:YES];
                [bgImage lt_XYcenterTo:nil save:YES];
                
                UILabel *remark = [[UILabel alloc]init];
                [cell addSubview:remark];
                remark.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
                remark.textColor = WHITE;
                remark.text = [data objectForKey:@"remark"];
                [remark lt_YcenterTo:nil save:YES];
                [remark lt_setLeft:X(36) to:nil save:YES];
                
                UILabel *l1 = [[UILabel alloc]init];
                [cell addSubview:l1];
                l1.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                l1.textColor = WHITE;
                l1.text = [data objectForKey:@"phone"];
                [l1 lt_XcenterTo:nil save:YES];
                [l1 lt_setTop:X(9) to:nil save:YES];
                UILabel *l2 = [[UILabel alloc]init];
                l2.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
                [cell addSubview:l2];
                l2.textColor = WHITE;
                [l2 lt_YcenterTo:nil save:YES];
                [l2 lt_setRight:X(29) to:nil save:YES];
                l2.text = [NSString stringWithFormat:@"%ld元",[[data objectForKey:@"money"] integerValue]];
                UILabel *l3 = [[UILabel alloc]init];
                [cell addSubview:l3];
                l3.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
                [l3 lt_XcenterTo:nil save:YES];
                [l3 lt_setBottom:X(8) to:nil save:YES];
                l3.textColor = WHITE;
                l3.text = [NSString stringWithFormat:@"20%@",[Sun timestamp:[[data objectForKey:@"time"] intValue] ToTime:@"YY/MM/dd HH:mm"]];
            }else if (indexPath.row == self.specialRecord.count) {
                UILabel *l1 = [[UILabel alloc]init];
                [cell addSubview:l1];
                l1.textColor = COLOR_TITLE_MINOR;
                l1.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                [l1 lt_YcenterTo:nil save:YES];
                [l1 lt_setLeft:X(41) to:nil save:YES];
                l1.text = @"投资用户";
                UILabel *l2 = [Sun copyWithView:l1];
                [cell addSubview:l2];
                [l2 lt_YcenterTo:nil save:YES];
                [l2 lt_XcenterTo:nil save:YES];
                l2.text = @"投资金额(元)";
                UILabel *l3 = [Sun copyWithView:l1];
                [cell addSubview:l3];
                [l3 lt_YcenterTo:nil save:YES];
                [l3 lt_setRight:X(37) to:nil save:YES];
                l3.text = @"投资时间";
            }else {
                NSDictionary *data = _recordArr[indexPath.row - self.specialRecord.count - 1];
                UILabel *l1 = [[UILabel alloc]init];
                [cell addSubview:l1];
                [l1 lt_YcenterTo:nil save:YES];
                [l1 lt_setLeft:X(30) to:nil save:YES];
                l1.font = [UIFont systemFontOfSize:13];
                l1.textColor = COLOR_TITLE_MAIN;
                l1.text = [data objectForKey:@"phone"];
                UILabel *l2 = [Sun copyWithView:l1];
                [cell addSubview:l2];
                [l2 lt_YcenterTo:nil save:YES];
                [l2 lt_XcenterTo:nil save:YES];
                l2.text = [NSString stringWithFormat:@"%ld",[[data objectForKey:@"money"] integerValue]];
                UILabel *l3 = [Sun copyWithView:l1];
                [cell addSubview:l3];
                [l3 lt_YcenterTo:nil save:YES];
                [l3 lt_setRight:X(15) to:nil save:YES];
                l3.textColor = COLOR_ALERT_DARK;
                l3.text = [NSString stringWithFormat:@"20%@",[Sun timestamp:[[data objectForKey:@"time"] intValue] ToTime:@"YY/MM/dd HH:mm"]];
            }
            
        }
    }//标详情
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == (_sessionArr.count == 2 ? 1 : 0)) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = WHITE;
        NSArray *arr = @[@"项目详情",@"安全保障",@"投资记录"];
        for (int i=0; i<3; i++) {
            UIButton *btn = [[UIButton alloc]init];
            [view addSubview:btn];
            btn.tag = i + 1;
            if (i == 2) {
                [btn setTitle:[NSString stringWithFormat:@"投资记录(%lu人)",_recordArr.count] forState:UIControlStateNormal];
            }else{
                [btn setTitle:arr[i] forState:UIControlStateNormal];
            }
            [btn setTitleColor:COLOR_TITLE_MAIN forState:UIControlStateNormal];
            [btn setTitleColor:COLOR_MAIN forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(changeDetail:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            [btn lt_setTop:0 to:nil Bottom:0 to:nil save:YES];
            [btn lt_setWidth:SCREENWIDTH / 3 save:YES];
            [btn lt_setLeft:SCREENWIDTH / 3 * i to:nil save:YES];
            if (_selectNum == i) {
                btn.selected = YES;
                UIView *slide = [[UIView alloc]init];
                [btn addSubview:slide];
                [slide lt_XcenterTo:nil save:YES];
                [slide lt_setSize:CGSizeMake(34, 3) save:YES];
                [slide lt_setBottom:0 to:nil save:YES];
                slide.backgroundColor = COLOR_MAIN;
            }
        }
        UIView *line = [[UIView alloc]init];
        [view addSubview:line];
        line.backgroundColor = LINE;
        [line lt_Bottom:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
        UIView *line2 = [Sun copyWithView:line];
        [view addSubview:line2];
        [line2 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
        return view;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BACKGROUND;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_sessionArr.count == 2 && indexPath.section == 0){
        if (![[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
            SunLoginVC *vc = [[SunLoginVC alloc]init];
            vc.isWeb = NO;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            NSString *string = [_sessionArr[0]objectForKey:@"content"];
            SunWebVC *vc = [[SunWebVC alloc]init];
            NSString *str = @"CZfSDQzsuDayjfzE";
            NSString *str1 =@"12345678";
            NSString *token_id=[Utility encryptStr:[NSString stringWithFormat:@"%@",[Sun getUser:ACCESSID]]  Key:str1 IV:nil];
            NSString *md5Str=[Utility md5:[NSString stringWithFormat:@"source=ios&token_id=%@&%@",token_id,str]];
            vc.urlStr = [NSString stringWithFormat:@"%@?source=ios&token_id=%@&sign=%@",string,token_id,md5Str];
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }//活动标题
    //    if ((_sessionArr.count == 2 && indexPath.section == 1) || (_sessionArr.count == 1 && indexPath.section == 0)) {
    //        if (indexPath.row == 0) {
    //            _showDetail = !_showDetail;
    //            [_tb reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    //        }
    //    }//还款详情
    //    if ((_sessionArr.count == 3 && indexPath.section == 3) || (_sessionArr.count == 2 && indexPath.section == 2)){
    //        if (self.canUseCard == 0) {
    //            return;
    //        }else if (![[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
    //            XSPLoginVC *vc = [[XSPLoginVC alloc]init];
    //            vc.isWeb = NO;
    //            [self.navigationController pushViewController:vc animated:YES];
    //        }else if (_cards.count > 0) {
    //            XSPUseCardsVC *vc = [[XSPUseCardsVC alloc]init];
    //            vc.cards = _cards;
    //            vc.money = _rulerValue;
    //            int time = 0;
    //            if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
    //                time = [[_borrowInfo objectForKey:@"days"] intValue];
    //            }else {
    //                time = [[_borrowInfo objectForKey:@"days"] intValue] * 30;
    //            }
    //            vc.days = time;
    //            [vc chooseCards:^(NSDictionary *cradDic) {
    //                _chooseCard = cradDic;
    //                [_tb reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    //            }];
    //            [self.navigationController pushViewController:vc animated:YES];
    //        }
    //    }//我的卡券
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
